//
//  LFAFHTTPClient.m
//  QingDaoCommunity
//
//  Created by 李学林 on 15/11/23.
//  Copyright © 2015年 teteTechnology. All rights reserved.
//

#import "LFAFHTTPClient.h"
#import "LFHomeModel.h"

@implementation LFAFHTTPClient
// 获取单例对象.
+(instancetype)sharedManager
{
    static LFAFHTTPClient *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
    });
    
    // 设置成通用的解析方式
    instance.responseSerializer = [AFHTTPResponseSerializer serializer];

    
    return instance;
}

+ (void)getURL:(NSString *)url WithParas:(NSDictionary *)paras WithRequestSuccess:(RequestSuccess)success WithFailled:(RequestFailed)failed{


    [[self sharedManager] getURL:url WithParas:paras WithRequestSuccess:success WithFailled:failed];
    

}

+ (void)postURL:(NSString *)url WithParas:(NSDictionary *)paras WithRequestSuccess:(RequestSuccess)success WithFailled:(RequestFailed)failed{


}

- (void)getURL:(NSString *)url WithParas:(NSDictionary *)paras WithRequestSuccess:(RequestSuccess)success WithFailled:(RequestFailed)failed{
    
    [self GET:url parameters:paras success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        //成功
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       //失败

        failed(error);
    }];

    
}

- (void)postURL:(NSString *)url WithParas:(NSDictionary *)paras WithRequestSuccess:(RequestSuccess)success WithFailled:(RequestFailed)failed{
    
    
}



//这是为了数据解析的
+ (void)getHomeWithParas:(NSDictionary *)paras WithRequestSuccess:(RequestSuccess)success WithFailled:(RequestFailed)failed{

    [[LFAFHTTPClient sharedManager]getHomeWithParas:paras WithRequestSuccess:success WithFailled:failed];

}
- (void)getHomeWithParas:(NSDictionary *)paras WithRequestSuccess:(RequestSuccess)success WithFailled:(RequestFailed)failed{

    [self getURL:@"?anu=api/1/index/get_index_info" WithParas:paras WithRequestSuccess:^(id obj) {
        //回调之后解析
        // 用一个plist文件数据来替代网络请求
        NSString *path = [[NSBundle mainBundle]pathForResource:@"home.plist" ofType:nil];

        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];

//        DSLog(@"%@",dict);

        LFHomeModel *model = [[LFHomeModel alloc]initWithDictionary:dict error:nil];
        
//        DSLog(@"%@",model.focus);

        
        // 解析完成之后再往后回调
        success(model);
        
        
    } WithFailled:^(NSError *error) {
        
//        //回调之后解析
//        // 用一个plist文件数据来替代网络请求
//        NSString *path = [[NSBundle mainBundle]pathForResource:@"home.plist" ofType:nil];
//        
//        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
//        
//        DSLog(@"%@",dict);
//        
//        LFHomeModel *model = [[LFHomeModel alloc]initWithDictionary:dict error:nil];
//        
//        DSLog(@"%@",model.focus);
//        
//        
//        // 解析完成之后再往后回调
//        success(model);
    }];

 
}

@end
